import {combineReducers} from 'redux';
import {ADD_COMMENT, DELETE_COMMENT, RECEIVE_COMMENTS} from './action-types'
// import {DECREMENT, INCREMENT} from "./action-types";

/*
    包含n个reducer函数（根据老的state和action返回一个新的state）
 */
const initComments = [];
const INCREMENT = "increment"
const DECREMENT = "decrement"

function comments(state = initComments, action) {
    // 切记原来转态不能改，得产生一个新的对象
    switch (action.type) {
        case ADD_COMMENT:
            // state.unshift(action.data); 这样子原有的就被删除了
            return [action.data, ...state];
        case DELETE_COMMENT:
            // [...state].slice(action.data, 1) 这个返回的是被删除的对象
            return state.filter(((comment, index) => index !== action.data));
        case RECEIVE_COMMENTS:
            // 这是初始化当前的state就是[] ，所以就不考虑
            return action.data;
        default:
            return state
    }

}

function counter(state = 0, action) {
    console.log("counter()", state, action);

    switch (action.type) {
        case INCREMENT:
            return state + action.data;
        case DECREMENT:
            return state - action.data;
        default:
            return state;
    }

}

export default combineReducers(
    {
        comments, // 指定reducer对应的属性
        counter
    }
)

// redux向外暴露的是个什么结构？
// {counter:2,comments:[]}